<?php

namespace App\Http\Controllers;

use App\Models\Capital;
use App\Models\Customer;
use EasySwoole\EasySwoole\Logger;
use EasySwoole\Http\Message\Status;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CommonController extends BaseController
{
    /**
     * 获取openid
     * GET /api/openid
     * @throws GuzzleException
     */
    public function openid(Request $request): \Illuminate\Http\JsonResponse
    {
        $params = $request->all();

        if (empty($params['code'])) {
            return response()->json(['status' => 0, 'message' => 'code不能为空']);
        }

        $corpId = 'wxe1472367879d72a2';
        $corpSecret = '50d1a823b83a422d19699c9659a67c5c';

        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $corpId . '&secret=' . $corpSecret . '&js_code=' . $params['code'] . '&grant_type=authorization_code';

        $client = new Client();
        $response = $client->request('GET', $url, [
            'headers' => ['Accept' => 'application/x-www-form-urlencoded', 'charset' => 'UTF-8']
        ]);
        $data = json_decode($response->getBody(), true);

        return response()->json(['status' => 1, 'data' => $data]);
    }

    /**
     * 登录
     * POST /api/login
     * @param Request $request
     * @return JsonResponse
     */
    public function login(Request $request): \Illuminate\Http\JsonResponse
    {
        try {
            $params = $request->all();
            if (empty($params['openid'])) {
                return response()->json(['status' => 0, 'message' => 'openid不能为空']);
            }

            DB::beginTransaction();

            $time = time();
            $customer = Customer::where('openid', $params['openid'])->first();
            if (empty($customer)) {
                Customer::create([
                    'openid' => $params['openid'],
                ]);
                $customer = Customer::where('openid', $params['openid'])->first();

                Capital::create([
                    'customer_id' => $customer->id,
                    'account_balance' => 0,
                    'point_balance' => 0,
                    'status' => 'NORMAL',
                ]);
                $customerId = $customer->id;
            } else {
                $customerId = $customer->id;
            }
            $token = Hash::make($customer->id . $time);

            Customer::where('id', $customerId)->update([
                'token' => $token,
                'login_num' => $customer->login_num + 1,
            ]);
            DB::commit();
            return response()->json(['status' => 1, 'data' => $token]);
        } catch (\Throwable $throwable) {
            DB::rollBack();
            return response()->json(['status' => 0, 'message' => $throwable->getMessage()]);
        }
    }
}
